# @Data  : 2019/11/27 14:59
# @Author: 
# @File  : wrapper.py
from decorator import decorator

from SeleniumLibrary import SeleniumLibrary
from SeleniumLibrary.keywords.formelement import FormElementKeywords

from Lib.zcm.common.ui.WaitPageLoad import *
from Lib.zcm.utils.screenshot import capture_page_screenshot
from Lib.zcm.utils.screenshot import _log_dir
from Lib.zcm.assistant import FwVariables

LOG_ROOT_PATH = os.path.join(os.path.split(os.path.abspath(__file__))[0].split("\Lib\zcm\\utils")[0], 'log')
EXCEPTION_PATH = os.path.join(_log_dir(), FwVariables.get_ef_name())
_selector_str_li = ['dialog-danger', 'dialog-warning']


def _currentDate():
    '''生成当前日期字符串'''
    return time.strftime("%Y%m%d", time.localtime())


def _currentTime():
    '''生成当前时间字符串'''
    return time.strftime("%H%M%S", time.localtime())


def _date_time():
    return time.strftime("%Y%m%d%H%M%S", time.localtime())


def _createDir():
    dateDir = os.path.join(LOG_ROOT_PATH, _currentDate())
    if not os.path.exists(dateDir):
        os.mkdir(dateDir)
    return dateDir


def _is_error_page_exist(browser):
    error_flag = False
    for selector_str in _selector_str_li:
        if PageElement(browser, selector_str).get_element_size() > 0:
            error_flag = True
            break
    return error_flag


# Modified by james at 20200814
# 把异常处理功能移到了 exception_handler 中
@decorator
def capture_screen_handler(func, filename='screenshot/zcm-screenshot-{index}.png', *args, **kwargs):
    try:
        return func(*args, **kwargs)
    finally:
        if FwVariables.isPicture() is True:
            try:
                if len(args) > 0 and isinstance(args[0], SeleniumLibrary):
                    capture_page_screenshot(args[0].driver, filename)
            except:
                traceback.print_exc()

if __name__ == "__main__":
    print(_date_time())
